package org.lionsoul.ip2region.txt;

import java.util.HashMap;
import java.util.Map;

public class Area {

    public static Area root = new Area();

    Integer id;
    String name;
    Integer type;
    Integer parentId;

    /**
     * 用id做索引
     */
    Map<Integer, Area> childrenIdMap = new HashMap<>(100);

    /**
     * 用名称做key
     */
    Map<String, Area> childrenNameMap = new HashMap<>(100);

    private Area() {
        id = 0;
        type = 0;
        parentId = 0;
    }

    private Area(String raw) {
        String[] cells = raw.split(",");
        this.id = Integer.parseInt(cells[0]);
        this.name = cells[1];
        this.type = Integer.parseInt(cells[2]);
        this.parentId = Integer.parseInt(cells[3]);
    }

    public static synchronized Area buildAreaTree(String raws) {
        Area parent = root;
        String[] areas = raws.split("\n");
        for (int i = 1; i < areas.length; i++) {
            Area a = new Area(areas[i]);
            if (a.parentId.equals(parent.id)) {
                putChild(parent, a);
            } else {
                // 切换parent
                // 先从上一级的平级找
                Area tmp  = parent.childrenIdMap.get(a.parentId);
                if (tmp!=null) {
                    parent = tmp;
                    putChild(parent, a);
                } else {
                    // 和当前的parent不是同一个祖父。直接从root开始找
                    if (a.parentId > 1000) {
                        // 中国下,两位两位来；
                        Area china = root.childrenIdMap.get(1);
                        int province = a.parentId / 1_00_00 * 1_00_00;
                        Area provinceArea = china.childrenIdMap.get(province);
                        int city = a.parentId % 1_00_00 / 1_00;
                        if (city == 0) {
                            // 到头了
                            parent = provinceArea;
                            putChild(parent, a);
                            continue;
                        }
                        city = a.parentId / 1_00 * 1_00;
                        Area cityArea = provinceArea.childrenIdMap.get(city);
                        // 目前也就到市级，区级已经是末尾了
                        parent = cityArea;
                        putChild(parent, a);
                    } else {
                        // 世界
                        parent = root;
                        putChild(parent, a);
                    }
                }
            }
        }
        return root;
    }

    private static void putChild(Area parent, Area a) {
        parent.childrenIdMap.put(a.id,a);
        if (parent.childrenNameMap.containsKey(a.name)) {
            // if exists, may throw exception is good?
            System.err.println("exist the same name area:[" + a.id + "," + a.name + "]");
        }
        parent.childrenNameMap.put(a.name, a);
    }

    public Area findCountry(String name) {
        return root.childrenNameMap.get(name);
    }

    public Area findProvince(String name) {
        // 在中国下找，找不到就null
        Area china = root.childrenIdMap.get(1);
        return china.childrenNameMap.get(name);
    }

    public Area findCity(String provinceName, String name) {
        Area province = findProvince(provinceName);
        if (null == province) {
            return null;
        }
        return province.childrenNameMap.get(name);
    }

}
